package org.example.gui.panel;

import org.example.gui.data.SpendData;
import org.example.gui.widget.BudgetProgressBar;
import org.example.util.ColorUtil;
import org.example.util.GUIUtil;

import javax.swing.*;
import java.awt.*;

/**
 * 界面类 SpendPanel 本月一览页，BorderLayout，边框布局
 */

public class SpendPanel extends WorkingPanel {
    public static SpendPanel instance = new SpendPanel();

    private final JLabel lMonthSpend = new JLabel("本月消费");
    private final JLabel lTodaySpend = new JLabel("今日消费");
    private final JLabel lAvgSpendPerDay = new JLabel("日均消费");
    private final JLabel lMonthLeft = new JLabel("本月剩余");
    private final JLabel lDayAvgAvailable = new JLabel("日均可用");
    private final JLabel lMonthLeftDay = new JLabel("距离月末");
    private final JLabel lUsagePercentageMonthly = new JLabel("月预算占比");
    private final JLabel lUsagePercentageDaily = new JLabel("日预算占比");


    private final JLabel vMonthSpend = new JLabel();
    private final JLabel vTodaySpend = new JLabel();
    private final JLabel vAvgSpendPerDay = new JLabel();
    private final JLabel vMonthAvailable = new JLabel();
    private final JLabel vDayAvgAvailable = new JLabel();
    private final JLabel vMonthLeftDay = new JLabel();


    private final BudgetProgressBar barMonthly;
    private final BudgetProgressBar barDaily;


    private SpendPanel() {
        this.setLayout(new BorderLayout());
        barMonthly = new BudgetProgressBar();
        barDaily = new BudgetProgressBar();
        barMonthly.setBackground(ColorUtil.blueColor);    // 原始颜色，0%时候，整个进度条都是蓝色的
        barDaily.setBackground(ColorUtil.blueColor);

        GUIUtil.setColor(ColorUtil.grayColor, lMonthSpend, lTodaySpend, lAvgSpendPerDay, lMonthLeft, lDayAvgAvailable,
                lMonthLeftDay, lUsagePercentageMonthly, lUsagePercentageDaily,
                vAvgSpendPerDay, vMonthAvailable, vDayAvgAvailable, vMonthLeftDay);
        GUIUtil.setColor(ColorUtil.blueColor, vMonthSpend, vTodaySpend);

        vMonthSpend.setFont(new Font("微软雅黑", Font.BOLD, 23));
        vTodaySpend.setFont(new Font("微软雅黑", Font.BOLD, 23));

        this.add(center(), BorderLayout.CENTER);
        this.add(south(), BorderLayout.SOUTH);

    }

    /**
     * SpendPanel 的 子面板 south
     * SpendPanel ==> south
     */
    private JPanel south() {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(4, 2, 5, 5));  // south面板 网格布局
        p.add(lAvgSpendPerDay);
        p.add(lMonthLeft);
        p.add(vAvgSpendPerDay);
        p.add(vMonthAvailable);
        p.add(lDayAvgAvailable);
        p.add(lMonthLeftDay);
        p.add(vDayAvgAvailable);
        p.add(vMonthLeftDay);
        return p;
    }

    /**
     * SpendPanel 的 子面板 center
     * SpendPanel ==> center
     */
    private JPanel center() {
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());    // center面板 边框布局
        p.add(centerEast(), BorderLayout.EAST);
        p.add(centerCenter(), BorderLayout.CENTER);
        return p;
    }

    /**
     * 子面板 center 的 子面板 centerEast
     * SpendPanel ==> center ==> centerEast
     */
    private Component centerEast() {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(6, 1));
        p.add(lMonthSpend);
        p.add(vMonthSpend);
        p.add(new JLabel());    // 用于布局，占位置，分割
        p.add(lTodaySpend);
        p.add(vTodaySpend);
        return p;
    }

    /**
     * 子面板 center 的 子面板 centerCenter
     * SpendPanel ==> center ==> centerCenter
     */
    private Component centerCenter() {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(6, 1));
        p.add(lUsagePercentageMonthly);
        p.add(barMonthly);
        p.add(new JLabel());    // 用于布局，占位置，分割
        p.add(lUsagePercentageDaily);
        p.add(barDaily);
        return p;
    }


    /**
     * 更新界面数据
     */
    @Override
    public void updateData() {
        SpendData spend = SpendData.getSpendData();
        vMonthSpend.setText(spend.monthSpend);
        vTodaySpend.setText(spend.todaySpend);
        vAvgSpendPerDay.setText(spend.avgSpendPerDay);
        vDayAvgAvailable.setText(spend.dayAvgAvailable);
        vMonthAvailable.setText(spend.monthAvailable);
        vMonthLeftDay.setText(spend.monthLeftDay);
        barMonthly.setProgress(spend.usagePercentageMonthly);     // 更新进度条
        barDaily.setProgress(spend.usagePercentageDaily);
        if (spend.isOverSpend) {
            // 如果超出预算，把本月剩余、本月消费、今日消费的字体改成红色（警告色）
            vMonthAvailable.setForeground(ColorUtil.warningColor);
            vMonthSpend.setForeground(ColorUtil.warningColor);
            vTodaySpend.setForeground(ColorUtil.warningColor);
        } else {
            vMonthAvailable.setForeground(ColorUtil.grayColor);
            vMonthSpend.setForeground(ColorUtil.blueColor);
            vTodaySpend.setForeground(ColorUtil.blueColor);
        }
        barMonthly.setForeground(ColorUtil.getByPercentage(spend.usagePercentageMonthly));
        barDaily.setForeground(ColorUtil.getByPercentage(spend.usagePercentageDaily));
        addListener();
    }

    @Override
    public void addListener() {

    }
}
